﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using VitCode.BaseDataPageCreator.Setting;
using VitCode.ObjectResolutionHelper;

namespace VitCode.BaseDataPageCreator
{
    public class ImportHelper
    {
        protected string m_SettingFilePath;
        protected ObjXmlReader m_XmlReader;

        public ObjXmlReader InnerXmlReader
        {
            get { return m_XmlReader; }
        }

        /*
         <Site KeyProperty="Id" AssemblyName="Demo.Core" NameSpace="Demo.Core.Entities" Memo="地点">
    <Columns>
      <item Name="Name" Memo="名称"></item>
      <item Name="Code" Memo="编码"></item>
      <item Name="Address" Memo="地址"></item>
      <item Name="Area.Name" Memo="所属地点名称"></item>
      <item Name="TypeName" Memo="类型名称"></item>
      <item Name="Department.Name"  Memo="所属部门" ></item>
      <item Name="Memo" Memo="备注"></item>
    </Columns>
    <Unique>
      <item Class="Site" ProNames="Name"></item>
      <item Class="Site" ProNames="Code"></item>
    </Unique>
    <Exists>
      <item Class="Area" ProNames="Area.Name"></item>
      <item Class="VitCode.Castle.UserGroup,VitCode.Castle" ProNames="Department.Name"></item>
    </Exists>
  </Site>
         */
        

        public ImportHelper(string settingFilePath)
        {
            if(string.IsNullOrEmpty(settingFilePath))
                throw new ArgumentNullException("settingFilePath");
            if (!File.Exists(settingFilePath))
                throw new ArgumentException(settingFilePath + "文件不存在");
            m_SettingFilePath = settingFilePath;
            m_XmlReader = new ObjXmlReader(m_SettingFilePath);
        }

        public XmlNodeList GetClassNodes()
        {
            XmlNodeList reVal = null;
            const string xPath = "Setting/*";
            reVal = m_XmlReader.GetNodeList(xPath);
            return reVal;
        }

        /// <summary>
        /// 获取导入参数
        /// </summary>
        /// <returns>Key=Memo,Value=ClassName,TypeName</returns>
        public NameValueCollection GetImprotTypeInfo()
        {
            var nvc = new NameValueCollection();
            var nl = GetClassNodes();
            foreach(XmlNode n in nl)
            {
                var strMemo = m_XmlReader.GetNodeAttributeValue(n, ImportKeyInfo.MEMO, true);
                if(!string.IsNullOrEmpty(strMemo))
                    nvc.Add(strMemo,n.Name);

                foreach(XmlNode cn in n.ChildNodes)
                {
                    if (cn.Name != ImportKeyInfo.COLUMNS && cn.Name != ImportKeyInfo.CHECKS)
                    {
                        strMemo = m_XmlReader.GetNodeAttributeValue(cn, ImportKeyInfo.MEMO, false);
                        nvc.Add(strMemo,n.Name + "," + cn.Name);
                    }
                }
            }
            return nvc;
        }

        public XmlNode GetImportObjNode(string className,string typeName)
        {
            XmlNodeList reVal = null;
            if (string.IsNullOrEmpty(className))
                throw new ArgumentNullException("className");
            var strTypeNamePath = "";
            if (!String.IsNullOrEmpty(typeName))
                strTypeNamePath = "/" + typeName;
            var xPath = "Setting/" + className + strTypeNamePath;
            reVal = m_XmlReader.GetNodeList(xPath);
            if (reVal == null || reVal.Count == 0)
                throw new ArgumentException("找不到ClassNode["+className+"]");
            if (reVal.Count > 1)
                throw new ArgumentException("找到多个ClassNode["+className+"]");
            return reVal[0];
        }

        public XmlNodeList GetColumnNodes(string className, string typeName)
        {
            XmlNodeList reVal = null;
            if (string.IsNullOrEmpty(className))
                throw new ArgumentNullException("className");
            var strTypeNamePath = "";
            if (!String.IsNullOrEmpty(typeName))
                strTypeNamePath = "/" + typeName;
            var xPath = "Setting/" + className + strTypeNamePath + "/" + ImportKeyInfo.COLUMNS + "/" + ImportKeyInfo.ITEM;
            reVal = m_XmlReader.GetNodeList(xPath);
            return reVal;
        }

        public XmlNodeList GetUniqueCheckNodes(string className, string typeName)
        {
            XmlNodeList reVal = null;
            if (string.IsNullOrEmpty(className))
                throw new ArgumentNullException("className");
            var strTypeNamePath = "";
            if (!String.IsNullOrEmpty(typeName))
                strTypeNamePath = "/" + typeName;
            var xPath = "Setting/" + className + strTypeNamePath + "/" + ImportKeyInfo.CHECKS + "/" +ImportKeyInfo.UNIQUE + "/" + ImportKeyInfo.ITEM;
            reVal = m_XmlReader.GetNodeList(xPath);
            return reVal;
        }

        public XmlNodeList GetExistsCheckNodes(string className, string typeName)
        {
            XmlNodeList reVal = null;
            if (string.IsNullOrEmpty(className))
                throw new ArgumentNullException("className");
            var strTypeNamePath = "";
            if (!String.IsNullOrEmpty(typeName))
                strTypeNamePath = "/" + typeName;
            var xPath = "Setting/" + className + strTypeNamePath + "/" + ImportKeyInfo.CHECKS + "/" + ImportKeyInfo.EXISTS + "/" + ImportKeyInfo.ITEM;
            reVal = m_XmlReader.GetNodeList(xPath);
            return reVal;
        }

        public XmlNodeList GetNotEmptyNodes(string className, string typeName)
        {
            XmlNodeList reVal = null;
            if (string.IsNullOrEmpty(className))
                throw new ArgumentNullException("className");
            var strTypeNamePath = "";
            if (!String.IsNullOrEmpty(typeName))
                strTypeNamePath = "/" + typeName;
            var xPath = "Setting/" + className + strTypeNamePath + "/" + ImportKeyInfo.CHECKS + "/" + ImportKeyInfo.EXISTS + "/" + ImportKeyInfo.ITEM + "[@MinLength>0]";
            reVal = m_XmlReader.GetNodeList(xPath);
            return reVal;
        }

        public XmlNode GetColumnItem(string className, string typeName, string columnName)
        {
            XmlNodeList reVal = null;
            if (string.IsNullOrEmpty(className))
                throw new ArgumentNullException("className");
            var strTypeNamePath = "";
            if (!String.IsNullOrEmpty(typeName))
                strTypeNamePath = "/" + typeName;
            var xPath = "Setting/" + className + strTypeNamePath + "/" + ImportKeyInfo.COLUMNS  + "/" + ImportKeyInfo.ITEM + "[@Name='" + columnName + "']";
            reVal = m_XmlReader.GetNodeList(xPath);
            if (reVal == null || reVal.Count == 0)
                throw new ArgumentException("找不到ColumnItem[" + columnName + "]");
            if (reVal.Count > 1)
                throw new ArgumentException("找到多个ColumnItem[" + columnName+ "]");
            return reVal[0];
        }

    }
}
